﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
    Sub Genera_Codigo()
        Dim SQL As String = "select Cod_Licitacion from Licitacion"
        Dim da As SqlDataAdapter
        Dim dt As New DataTable
        Try
            da = New SqlDataAdapter(SQL, nCnn)
            da.Fill(dt)
            TextBox1.Text = Format(Val(dt.Rows.Count) + 1, "0000")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Carga_Preguntas()
        Dim SQL As String = "SELECT * FROM  licitacion_pregunta WHERE Cod_licitacion='" & TextBox1.Text & "'" & " ORDER BY Cod_licitacion, Cod_Pregunta ASC"
        Dim da As SqlDataAdapter
        Dim dt As New DataTable
        Try
            da = New SqlDataAdapter(SQL, nCnn)
            da.Fill(dt)
            ListView1.Items.Clear()
            For i = 0 To dt.Rows.Count - 1
                Dim itm = ListView1.Items.Add("")
                itm.SubItems.Add(RTrim(dt.Rows(i)("Pregunta")))
                itm.SubItems.Add(RTrim(dt.Rows(i)("Peso")))
            Next
            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
        Catch ex As Exception
            MsgBox("Carga preguntas: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        TextBox3.Text = Usuario
        TextBox3.ReadOnly = True
        TextBox1.Focus()
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
        If Asc(e.KeyChar) = 13 Then
            DateTimePicker1.Focus()
        End If
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        TextBox2.Text = StrConv(TextBox2.Text, VbStrConv.Uppercase)
        TextBox2.SelectionStart = Len(TextBox2.Text)
    End Sub

    Private Sub DateTimePicker1_CloseUp(sender As Object, e As EventArgs) Handles DateTimePicker1.CloseUp
        TextBox3.Focus()
    End Sub

    Private Sub DateTimePicker1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DateTimePicker1.KeyPress
        If Asc(e.KeyChar) = 13 Then
            TextBox6.Focus()
        End If
    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged

    End Sub

    Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
        If Asc(e.KeyChar) = 13 Then
            Button1.Enabled = True
            Button1.Focus()
        End If
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        TextBox3.Text = StrConv(TextBox3.Text, VbStrConv.Uppercase)
        TextBox3.SelectionStart = Len(TextBox3.Text)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim resp = MsgBox("¿ Desea registrar la licitación ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If resp = vbYes Then
            '-------Registra nueva licitacion
            Dim Estado As String = "01"
            Dim SQL As String = "INSERT INTO Licitacion(Cod_Licitacion, Descripcion, Fecha_registro, Fecha_fin, Responsable, Cod_estado) values('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & Format(CType(Date.Today, Date), "MM/dd/yyyy") & "', '" & Format(CType(DateTimePicker1.Value, Date), "MM/dd/yyyy") & "', '" & TextBox3.Text & "', '" & Estado & "'" & ")"
            Conn.Open()
            Try
                Dim cdo As New SqlCommand(SQL, Conn)
                cdo.ExecuteNonQuery()
                '------Registra listado de preguntas
               

            Catch ex As Exception
                MsgBox("Registra licitacion: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            Conn.Close()

            Dim cod As String
            For i = 0 To ListView1.Items.Count - 1
                cod = Format(Val(i) + 1, "00")
                Dim SQL1 As String = "INSERT INTO Licitacion_pregunta(Cod_licitacion, cod_pregunta, pregunta, peso, cod_estado) values('" & TextBox1.Text & "', '" & cod & "', '" & ListView1.Items(i).SubItems(1).Text & "', '" & ListView1.Items(i).SubItems(2).Text & "', '" & Estado & "'" & ")"
                Conn.Open()
                Try
                    Dim cdo1 As New SqlCommand(SQL1, Conn)
                    cdo1.ExecuteNonQuery()
                Catch ex As Exception
                    MsgBox("Registra preguntas: " & ex.Message, MsgBoxStyle.Critical)
                End Try
                Conn.Close()
            Next

            MsgBox("Registro exitoso", MsgBoxStyle.Information)
            Me.Close()
        End If
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        Select Case Asc(e.KeyChar)
            Case 47 To 58
            Case 8
            Case 13
                If Len(TextBox1.Text) <> 0 Then
                    TextBox1.Text = Format(Val(TextBox1.Text), "0000")
                    Dim SQL As String = "select * from Licitacion WHERE Cod_licitacion='" & TextBox1.Text & "'"
                    Dim da As SqlDataAdapter
                    Dim dt As New DataTable
                    Try
                        da = New SqlDataAdapter(SQL, nCnn)
                        da.Fill(dt)
                        If dt.Rows.Count <> 0 Then
                            If RTrim(dt.Rows(0)("Cod_Estado")) = "01" Then
                                Call Carga_Preguntas()
                                TextBox2.Text = RTrim(dt.Rows(0)("Descripcion"))
                                DateTimePicker1.Value = dt.Rows(0)("fecha_fin")
                                TextBox3.Text = RTrim(dt.Rows(0)("Responsable"))
                                Button1.Enabled = False
                                Button4.Enabled = True
                                TextBox2.Focus()
                            Else
                                MsgBox("El estado de la licitación no permite su modificación", MsgBoxStyle.Exclamation)
                                TextBox1.Text = ""
                                TextBox1.Focus()
                            End If
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical)
                    End Try
                Else
                    Call Genera_Codigo()
                    Button1.Enabled = True
                    Button4.Enabled = False
                    TextBox2.Focus()
                End If

            Case Else
                e.Handled = True
        End Select
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim Itm = ListView1.Items.Add("")
        Itm.SubItems.Add(TextBox6.Text)
        Itm.SubItems.Add(NumericUpDown1.Value)
        ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
        Dim resp = MsgBox("¿ Desea agregar otra pregunta ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If resp = vbYes Then
            TextBox6.Text = ""
            NumericUpDown1.Value = 0
        Else
        End If
    End Sub

    Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub TextBox6_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox6.KeyPress
        If Asc(e.KeyChar) = 13 Then
            NumericUpDown1.Focus()
        End If
    End Sub

    Private Sub TextBox6_TextChanged(sender As Object, e As EventArgs) Handles TextBox6.TextChanged
        TextBox6.Text = StrConv(TextBox6.Text, VbStrConv.Uppercase)
        TextBox6.SelectionStart = Len(TextBox6.Text)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim resp = MsgBox("¿ Desea actualizar la licitación ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If resp = vbYes Then
            '------Actualiza licitaciones
            Dim SQL As String = "UPDATE Licitacion SET Descripcion='" & TextBox2.Text & "', fecha_registro='" & Format(CType(Date.Today, Date), "MM/dd/yyyy") & "', fecha_fin='" & Format(CType(DateTimePicker1.Value, Date), "MM/dd/yyyy") & "'" & " WHERE Cod_licitacion='" & TextBox1.Text & "'"
            Conn.Open()
            Try
                Dim cdo As New SqlCommand(SQL, Conn)
                cdo.ExecuteNonQuery()

            Catch ex As Exception
                MsgBox("Actualiza licitacion: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            Conn.Close()

            '-----Elimina Preguntas Registradas
            Dim SQL1 As String = "delete from licitacion_pregunta where Cod_licitacion='" & TextBox1.Text & "'"
            Conn.Open()
            Try
                Dim cdo1 As New SqlCommand(SQL1, Conn)
                cdo1.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox("Limpia preguntas: " & ex.Message, MsgBoxStyle.Critical)
            End Try
            Conn.Close()

            '-----Registra todas las preguntas nuevamente
            Dim cod As String
            Dim Estado As String = "01"
            For i = 0 To ListView1.Items.Count - 1
                cod = Format(Val(i) + 1, "00")
                Dim SQL2 As String = "INSERT INTO Licitacion_pregunta(Cod_licitacion, cod_pregunta, pregunta, peso, cod_estado) values('" & TextBox1.Text & "', '" & cod & "', '" & ListView1.Items(i).SubItems(1).Text & "', '" & ListView1.Items(i).SubItems(2).Text & "', '" & Estado & "'" & ")"
                Conn.Open()
                Try
                    Dim cdo2 As New SqlCommand(SQL2, Conn)
                    cdo2.ExecuteNonQuery()
                Catch ex As Exception
                    MsgBox("Ingresa nuevas preguntas: " & ex.Message, MsgBoxStyle.Critical)
                End Try
                Conn.Close()
            Next

            MsgBox("Actualización exitosa", MsgBoxStyle.Information)
            Me.Close()
        Else
            Me.Close()
        End If


    End Sub

    Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Checked = True Then
                ListView1.Items(i).SubItems(0).BackColor = Color.Blue
                ListView1.Items(i).SubItems(0).ForeColor = Color.White
            Else
                ListView1.Items(i).SubItems(0).BackColor = Color.White
                ListView1.Items(i).SubItems(0).ForeColor = Color.Black
            End If
        Next
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Checked = True Then
                ListView1.Items(i).Remove()
                Exit For
            End If
        Next
    End Sub
End Class